package com.xxpt.common.constant;

import lombok.Data;

import java.util.HashMap;
import java.util.Map;

@Data
public class UserContext {
    public static final ThreadLocal<Map<String, Object>> USER_DATA = new ThreadLocal<Map<String, Object>>();
    //来源 1-app 2-pc 3-h5
    public static final String USER_AGENT_SOURCE_KEY = "source";
    //设备类型 ios1.安卓2 h5 3
    public static final String DEVICE_TYPE_KEY = "deviceType";
    public static final String USER_ID_KEY = "userId";
    //app版本
    public static final String USER_AGENT_VERSION_KEY = "version";
    //手机型号
    public static final String USER_AGENT_MODEL_KEY = "model";
    public static final String REQUEST_ID_KEY = "requestId";
    public static final String AUTHORIZE_TOKEN = "Authorization";
    public static final String LANGUAGE_KEY = "language";
    //操作系统
    public static final String USER_AGENT_OS_KEY = "os";
    //操作系统版本
    public static final String USER_AGENT_OS_VERSION_KEY = "osVersion";
    //性别
    public static final String GENDER_KEY = "gender";

    public static Integer getDeviceType() {
        Object deviceType = get(UserContext.DEVICE_TYPE_KEY);
        if (deviceType == null) {
            return null;
        } else {
            return Integer.valueOf(deviceType.toString());
        }
    }

    public static Long getUserId() {
        return (Long) get(UserContext.USER_ID_KEY);
    }

    public static String getVersion() {
        return (String) get(UserContext.USER_AGENT_VERSION_KEY);
    }
    public static String getRequestId(){ return (String) get(UserContext.REQUEST_ID_KEY);}
    public static String getSource(){ return (String) get(UserContext.USER_AGENT_SOURCE_KEY);}
    public static String getToken() {
        return (String) get(UserContext.AUTHORIZE_TOKEN);
    }
    public static String getModel(){ return (String) get(UserContext.USER_AGENT_MODEL_KEY);}

    public static String getDevice() {
        return (String) get(UserContext.USER_AGENT_OS_KEY);
    }
    public static String getOsVersion(){ return (String) get(UserContext.USER_AGENT_OS_VERSION_KEY);}

    public static Integer getGender(){
        Object o = get(UserContext.GENDER_KEY);
        if (o != null){
            return Integer.valueOf(o.toString());
        }
        return 1;
    }


    public static void init() {
        USER_DATA.set(new HashMap<>());
    }

    public static Object get(String key) {
        Map<String, Object> userDataMap = USER_DATA.get();
        if (userDataMap == null) {
            return null;
        }
        return userDataMap.get(key);
    }

    public static Map<String, Object> getThreadLocalMap() {
        return USER_DATA.get();
    }

    public static void put(String key, Object value) {
        USER_DATA.get().put(key, value);
    }

    public static void remove() {
        USER_DATA.remove();
    }
}
